//
//  SINATecMainItemTableViewCell.m
//  sinashebao
//
//  Created by  androidlongs on 2018/3/15.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecMainItemTableViewCell.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"


@interface SINATecMainItemTableViewCell ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *startSignLabel;
@property (weak, nonatomic) IBOutlet UIView *isAlertCourseView;

//调课
@property (weak, nonatomic) IBOutlet UIView *alertCoursePrentView;
@property (weak, nonatomic) IBOutlet UILabel *alertCourseLabel;

//标题
@property (weak, nonatomic) IBOutlet UILabel *coureTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *coureTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;

//查看点名
@property (weak, nonatomic) IBOutlet UILabel *showSignListLabel;

@end
@implementation SINATecMainItemTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    [SINACircularBeadUtils makeCircularBead:2 andView:self.mainView];
    [SINACircularBeadUtils makeCircularBead:2 andView:self.alertCoursePrentView];
    [SINACircularBeadUtils makeCircularBead:2 andView:self.isAlertCourseView];
    [SINACircularBeadUtils makeCircularBead:2 andView:self.startSignLabel andIsSetBackground:YES];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)setModel:(SINASubClassModel *)model{
    _model = model;
    
    self.coureTitleLabel.text=model.name;
    self.coureTimeLabel.text=[NSString stringWithFormat:@"%@-%@  %@",[SINACalendarUtils getDateStringWithTimeStr:model.starttime andFormat:@"HH:mm"],[SINACalendarUtils getDateStringWithTimeStr:model.endtime andFormat:@"HH:mm"],model.classifyname];
    
    
    //classroom
    self.addressLabel.text=[NSString stringWithFormat:@"%@ %@",model.buildname,model.classroom];
    
    
    
    
    //当前时间
    NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
    DLog(@"课程签到当前时间是 %ld",(long)currentTime);
    
    //课程开始时间
    NSInteger courseStartTime = [model.starttime integerValue];
    DLog(@"课程签到当前时间是 courseStartTime %ld",(long)courseStartTime);
    //课程结束时间
    NSInteger courseEndTime = [model.endtime integerValue];
    
    //开课前50分钟可签到
    NSInteger signStartTime = courseStartTime -1000*courseSignPreTime*60;
//    NSInteger signEndTime = courseStartTime +1000*courseSignPreTime*60;
    
    
    
    
    
    if(currentTime<signStartTime){
        //未到签到时间
        [self.startSignLabel setBackgroundColor:[UIColor colorWithHexString:@"#E0E0E0"]];
    }else if (currentTime>=courseStartTime&&currentTime<=courseEndTime){
        //可点名时间中
        [self.startSignLabel setBackgroundColor:[UIColor colorWithHexString:@"#4F8FD7"]];
    }else{
        [self.startSignLabel setBackgroundColor:[UIColor colorWithHexString:@"#E0E0E0"]];
    }
    
    
    
    
    
    if ([model.ischange integerValue]==2) {
        //已调课
        self.isAlertCourseView.hidden = NO;
    } else {
        self.isAlertCourseView.hidden = YES;
        //未调课
        if (self.itemBlock) {
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.showSignListLabel andWithBlick:^(UIView *view) {
                self.itemBlock(self.model,0);
            }];
            
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.alertCourseLabel andWithBlick:^(UIView *view) {
                if (self.alertCoursePrentView.hidden==NO) {
                    [UIView animateWithDuration:0.45 animations:^{
                        self.mainView.frame = CGRectMake(10,  CGRectGetMinY(self.mainView.frame),CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame));
                    } completion:^(BOOL finished) {
                        self.alertCoursePrentView.hidden=YES;
                        
                        
                        //当前时间
                        NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
                        DLog(@"课程签到当前时间是 %ld",(long)currentTime);
                        
                        //课程开始时间
                        NSInteger courseStartTime = [self.model.starttime integerValue];
                        DLog(@"课程签到当前时间是 courseStartTime %ld",(long)courseStartTime);
                        //课程结束时间
                        NSInteger courseEndTime = [self.model.endtime integerValue];
                        
                        if (currentTime>courseStartTime&&currentTime<=courseEndTime) {
                            //课程开始 课程结束 不可调课
                            [YPXToastView showToastViewWithText:@"课程已开始 不可调课" andDuration:2 andParentView:self.mainView];
                            return;
                        }else if (currentTime>courseEndTime){
                            [YPXToastView showToastViewWithText:@"课程已结束 不可调课" andDuration:2 andParentView:self.mainView];
                            return;
                        }
                        
                        //调课
                        self.itemBlock(self.model,1);
                    }];
                    
                }
            }];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:self.startSignLabel andWithBlick:^(UIView *view) {
                //点名
                self.itemBlock(self.model,2);
            }];
        }
        
        [[SINAUiviewClickLiserner alloc]viewClickFunction:self.mainView andWithBlick:^(UIView *view) {
            [UIView animateWithDuration:0.45 animations:^{
                self.mainView.frame = CGRectMake(10,  CGRectGetMinY(self.mainView.frame),CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame));
            } completion:^(BOOL finished) {
                self.alertCoursePrentView.hidden=YES;
            }];
        }];
        
        //课程调课 左滑删除
        
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(itemView4HandleSwipeFrom:)];
        //设置属性，swipe也是有两种属性设置手指个数及轻扫方向
        swipe.numberOfTouchesRequired = 1;
        //设置轻扫方向(默认是从左往右)
        //direction是一个枚举值有四个选项，我们可以设置从左往右，从右往左，从下往上以及从上往下
        //设置轻扫方向(默认是从左往右)
        [swipe setDirection:((void)(UISwipeGestureRecognizerDirectionLeft),UISegmentedControlSegmentRight)];
        [self.mainView addGestureRecognizer:swipe];
        
        
    }
    
    
    
}

- (void)itemView4HandleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    
    if (recognizer.view==self.mainView) {
        
        if(CGRectGetMinX(self.mainView.frame)>-50) {
            DLog(@"swipe left");
            self.alertCoursePrentView.hidden=NO;
            [UIView animateWithDuration: 0.45 animations: ^{
                self.mainView.frame = CGRectMake(CGRectGetMinX(self.mainView.frame)-100, CGRectGetMinY(self.mainView.frame),CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame));
            } completion: nil];
            
        }else {
            DLog(@"swipe right");
            [UIView animateWithDuration:0.45 animations:^{
                self.mainView.frame = CGRectMake(10,  CGRectGetMinY(self.mainView.frame),CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame));
            } completion:^(BOOL finished) {
                self.alertCoursePrentView.hidden=YES;
            }];
        }
    }
    
}

@end
